<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Port;//港口模型
use think\Db;
use app\admin\model\Container_carrier;//承运商模型
use app\admin\model\Container;//货柜模型


class Logistics extends Auth
{
    //港口的列表
    public function port(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        if ($request->isAjax() && $request->isPost()) {
            $port = new Port();
            $getports = $port->getports($res);
            echo json_encode(array('getports' => $getports['data'], 'size' => $getports['count']));
        } elseif ($request->isGet()) {
            return view('port/port');
        }
    }

    //港口的添加
    public function portadd(Request $request)
    {
        $data = $request->param();
        $port = new Port();
        $country = getables('country');
        if ($request->isGet()) {
            return view('port/portinfo', [
                'str' => 'portadd',
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'portadd') {
            $portret = $port->addeditport($data);
            $udata["status"] = $portret['status'];
            $udata['msg'] = $portret['msg'];
            echo json_encode($udata);
        }
    }

    //港口的编辑
    public function portedit(Request $request)
    {
        $data = $request->param();
        $port = new Port();
        if ($request->isGet()) {
            $info = getinfo($data['id'], 'port');
            $country = getables('country');
            return view('port/portinfo', [
                'str' => 'portedit',
                'info' => $info,
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'portedit') {
            $portret = $port->addeditport($data);
            $udata["status"] = $portret['status'];
            $udata['msg'] = $portret['msg'];
            echo json_encode($udata);
        }

    }

    //港口的删除
    public function portdel(Request $request)
    {
        $data = $request->param();
        $port = new Port();
        if ($request->isPost() && $request->isAjax()) {
            $portret = $port->delport($data);
            $udata["status"] = $portret['status'];
            $udata['msg'] = $portret['msg'];
            echo json_encode($udata);
        }
    }

    //承运商列表
    public function carrier(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            $carrier = new Container_carrier();
            $getcarriers = $carrier->getcarriers($res);
            echo json_encode(array('getcarriers' => $getcarriers['data'], 'size' => $getcarriers['count']));
        } elseif ($request->isGet()) {
            return view('carrier/carrier');
        }
    }

    //承运商的添加
    public function carrieradd(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $carrier = new Container_carrier();
        if ($request->isGet()) {
            return view('carrier/carrierinfo', [
                'str' => 'carrieradd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'carrieradd') {
            $carrierret = $carrier->addeditcarrier($data);
            $udata["status"] = $carrierret['status'];
            $udata['msg'] = $carrierret['msg'];
            echo json_encode($udata);
        }
    }

    //承运商的编辑
    public function carrieredit(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $carrier = new Container_carrier();
        if ($request->isGet()) {
            return view('carrier/carrierinfo', [
                'str' => 'carrieredit',
                'id' => $data['id'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getcarrier'])) {
            //获取对应的语言的公司类型
            $where['id'] = $data['id'];
            $info = getlanginfo('container_carrier', $where);
            echo json_encode(array('info' => $info));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'carrieredit') {
            $carrierret = $carrier->addeditcarrier($data);
            $udata["status"] = $carrierret['status'];
            $udata['msg'] = $carrierret['msg'];
            echo json_encode($udata);
        }

    }

    //承运商的删除
    public function carrierdel(Request $request)
    {
        $carrier = new Container_carrier();
        $data = $request->param();
        if ($request->isPost() && $request->isAjax()) {
            $carrierret = $carrier->delcarrier($data);
            $udata["status"] = $carrierret['status'];
            $udata['msg'] = $carrierret['msg'];
            echo json_encode($udata);
        }
    }

    //货柜列表
    public function container(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        $container = new Container();
        if ($request->isGet()) {
            return view('container/container');
        } elseif ($request->isPost() && $request->isAjax()) {
            //货柜列表的接口数据
            $getcontainers = $container->getcontainer($res);
            echo json_encode($getcontainers);
        }
    }

    //添加新货柜
    public function containeradd(Request $request)
    {
        $container = new Container();
        $data = $request->param();
        $lang = $this->lang;
        if ($request->isGet()) {
            //处理准备日期
            $dateday = getTimeDate(null, 7);
            //名称
            $name = 'NT' . date('ymd', time());
            $datares = array(
                'dateday' => $dateday,//日期
                'container_type' => getables('container_type', [], 'id,name'),//货柜类型
                'port_start' => getables('port', ['country_id' => 2], 'id,name'),//出发地
                'port_arrival' => getables('port', ['country_id' => 1], 'id,name'), //目标地
                'co_id_start' => getables('container_agent', ['country_id' => 2], 'id,name'),// 货贷(中国)
                'co_id_arrival' => getables('container_agent', ['country_id' => 1], 'id,name'),  //货贷(巴西)
                'name' => $name
            );
            return view('container/containeradd', [
                'data' => $datares
            ]);
        } elseif ($request->isAjax() && $request->isPost()) {
            //添加货柜的接口
            $containerret = $container->containeradd($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }

    //编辑货柜
    public function containeredit(Request $request)
    {
        $data = $request->param();
        $lang = $this->lang;
        $container = new Container();
        if ($request->isGet()) {
            return view('container/containeredit', ['id' => $data['id']]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['geteditinfo'])) {
            $resdata = $container->editinfo($data, $lang);
            echo json_encode($resdata);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['savedata'])) {
            $containerret = $container->containeredit($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }

    //批量调整状态
    public function containerchang(Request $request)
    {
        $data = $request->param();
        $container = new Container();
        if ($request->isGet()) {
            $lang = $this->lang;
            $resdata = $container->getchange($data, $lang);
            $ids = $data['ids'];
            return view('container/containerchange', [
                'resdata' => $resdata,
                'ids' => $ids,
            ]);
        } elseif ($request->isPost() && $request->isAjax()) {
            $containerret = $container->containerchang($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }

    //货柜打印
    public function preview(Request $request)
    {
        $cookiedata = cookie('dayinset');
        $container = new Container();
        $res = $request->param();
        if ($request->isPost() && $request->isAjax() && isset($res['setdata'])) {
            cookie('dayinset', $res['setddatas']);//将打印的设置存储起来
        } elseif ($request->isGet() && isset($res['onetwothree'])) {
            return view('container/preview');
        }  elseif ($request->isGet() && isset($res['twoformat'])) {
            //excel
            if ($cookiedata['format'] == 2) {
                $id = $cookiedata['container_id'];
                $head = ['NO.', ' ', '产品名称', '箱数', '每件数量', '箱规', '重量', '密度', '数量合计', '体积合计', '重量合计'];
                $separate = $cookiedata['separate'];//是否分割
                $lang = $cookiedata['langname'];
                $datares['prodata'] = $this->getExeclPoData($id, $separate, $lang);
                $id = $cookiedata['container_id'];
                $info = getinfo($id, 'container', 'id,name,code,arrival_day');
                $info['arrival_day'] = getTimeDate($info['arrival_day'], 7);
                $datares['huodata'] = $info;
                $title = $info['name'] . '_' . strtoupper($this->lang);
                //打印设置
                $fontSize = $cookiedata['fontSize'];//字体大小
                $iconSize = $cookiedata['iconSize'];//产品图片
                $font = 14;
                $iconimg = array('width' => 40, 'height' => 40);
                if ($fontSize == 1) {
                    //小
                    $font == 14;
                } elseif ($fontSize == 2) {
                    //中
                    $font == 16;
                } elseif ($fontSize == 3) {
                    //大
                    $font == 18;
                }
                if ($iconSize == 1) {
                    //小
                } elseif ($iconSize == 2) {
                    //中
                    $iconimg == array('width' => 40, 'height' => 40);
                } elseif ($iconSize == 3) {
                    //大
                    $iconimg == array('width' => 60, 'height' => 60);
                } elseif ($iconSize == 4) {
                    //大
                    $iconimg == array('width' => 80, 'height' => 80);
                }
                export($title, $head, $datares, $separate, $font, $iconimg);
            }
        }  elseif ($request->isPost() && $request->isAjax() && isset($res['onethree'])) {
            //请求获取数据
            $datares = $container->getcontainerdayi($cookiedata);
            echo json_encode($datares);
        }
    }

    /**
     *   $container_id   货柜id
     *   $requestType    是否分隔 1是，2否
     *   $drank          排序规则
     **/
    function getExeclPoData($container_id, $requestType, $lang, $drank = '1=1')
    {
        $ids = Db::name('buy_po_pack')->field(['po_id'])->where(['container_id' => $container_id])->group('po_id')->select();
        $po_ids = array_column($ids, 'po_id');
        $execlPoData = [];
        if (!empty($po_ids)) {
            foreach ($po_ids as $key => $value) {
                $where['zu.id'] = $value;
                $returnData = model('Buy')->getBuyPo($where, $lang);

                if (!empty($returnData)) {
                    foreach ($returnData as $k => $v) {
                        if ($requestType != 1) {
                            $execlPoData[$key]['po_id'] = $v['id'];
                            $execlPoData[$key]['company_name'] = $v['company_name'];
                            $execlPoData[$key]['delivery_day'] = $v['delivery_day'];
                        }
                        if (!empty($v['buy_plan'])) {
                            foreach ($v['buy_plan'] as $k1 => $v1) {
                                $res['img_url'] = $v1['img_url'];
                                $res['pro_id'] = $v1['pro_id'];
                                $res['prochar_id'] = $v1['prochar_id'];
                                $res['pro_name'] = $v1['pro_name'];
                                $res['brand_name'] = $v1['brand_name'];
                                $res['model'] = $v1['model'];
                                $res['qty'] = $v1['qty'];
                                $res['ctn_qty'] = $v1['ctn_qty'];
                                $res['ctn_size'] = $v1['ctn_size'];
                                $res['ctn_kgs'] = $v1['ctn_kgs'];
                                $res['ctn_m3'] = $v1['ctn_m3'];
                                $res['qty_sum'] = $v1['qty_sum'];
                                $res['m3_sum'] = $v1['m3_sum'];
                                $res['ksg_sum'] = $v1['kgs_sum'];
                                if ($requestType != 1) {
                                    $execlPoData[$key]['pro'][$k1] = $res;
                                } else {
                                    $execlPoData[] = $res;
                                }
                            }
                        }
                    }
                }
            }
        }
        return $execlPoData;
    }


    //处理货柜详情查看
    public function containerdetail(Request $request)
    {
        $data = $request->param();
        $container = new Container();
        $lang = $this->lang;
        if ($request->isGet()) {
            return view('container/detail');
        } elseif ($request->isAjax() && $request->isPost() && isset($data['cdetail'])) {
            $resdata = $container->getdetail($data, $lang);
            echo json_encode($resdata);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['wplace'])) {
            //根据仓库id获取对应的仓库位置id
            $warehouse_place = getables('warehouse_place', ['warehouse_id' => $data['warehouse_id']], 'id,name');
            echo json_encode($warehouse_place);
        }
    }

    //处理货柜的入仓
    public function containerarrival(Request $request)
    {
        $data = $request->param();
        $container = new Container();
        $lang = $this->lang;
        if ($request->isGet()) {
            return view('container/arrival');
        } elseif ($request->isAjax() && $request->isPost() && isset($data['setstrong'])) {
            //入仓
            $containerret = $container->setstrong($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['setcancel'])) {
            //取消入仓
            $resdata = $data['setcanceldata'][0];
            unset($data['setcanceldata']);
            $resdata['container_id'] = $data['container_id'];
            $containerret = $container->setcancel($resdata);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }

    //开航接口
    public function etdday(Request $request)
    {
        $data = $request->param();
        $container = new Container();
        if ($request->isPost() && $request->isAjax()) {
            $containerret = $container->etdetaarrivalday($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }

    //到达接口
    public function etaday(Request $request)
    {
        $data = $request->param();
        $container = new Container();
        if ($request->isPost() && $request->isAjax()) {
            $containerret = $container->etdetaarrivalday($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }

    //货柜删除的接口
    public function containerdel(Request $request)
    {
        $data = $request->param();
        $container = new Container();
        if ($request->isPost() && $request->isAjax()) {
            $containerret = $container->delcontainer($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }

    //货柜抵达列表
    public function arrive()
    {
        return $this->redirect('/admin/logistics/container?status=4');
    }
}